package tw.com.book.core.task01;

import org.apache.commons.lang3.SerializationUtils;

import tw.com.book.api.task01.MainTaskService;
import tw.com.book.componet.DBHelper;
import tw.com.book.componet.IssueBussinessException;
import tw.com.book.domain.V002Type;
import tw.com.book.domain.dao.V001DAO;
import tw.com.book.domain.dao.V002DAO;
import tw.com.book.dto.MainDTO;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MainTaskServiceImpl implements MainTaskService {

	private static final String TAG = MainTaskServiceImpl.class
			.getCanonicalName();

	@Override
	public void doQuery(MainDTO dto, Context context)
			throws IssueBussinessException {

		final DBHelper help = new DBHelper(context);

		final SQLiteDatabase conn = help.getWritableDatabase();

		try {
			V001DAO dao = new V001DAO(conn, context);

			dto.setV001s(dao.selectAll());
		} catch (Exception e) {

		} finally {
			conn.close();
		}
		// TODO Auto-generated method stub

	}

	@Override
	public void doV002(MainDTO dto, Context context)
			throws IssueBussinessException {
		final DBHelper help = new DBHelper(context);

		final SQLiteDatabase conn = help.getWritableDatabase();

		try {
			V002DAO dao = new V002DAO(conn, context);

			dto.setSelectV002(dao.selectOne(dto.getUuid()));
		} catch (Exception e) {

		} finally {
			conn.close();
		}

	}

	@Override
	public void doV002s(MainDTO dto, Context context)
			throws IssueBussinessException {
		final DBHelper help = new DBHelper(context);

		final SQLiteDatabase conn = help.getWritableDatabase();

		try {
			V002DAO dao = new V002DAO(conn, context);

			dto.setV002s(dao.select(dto.getUuid()));

			dto.setSelectV002(dto.getV002s().get(0));
		} catch (Exception e) {

		} finally {
			conn.close();
		}

	}

	@Override
	public void pendContent(MainDTO dto, Context context)
			throws IssueBussinessException {

		int no = Integer.parseInt(dto.getSelectV002().getSerialNo());

		final V002Type v002 = SerializationUtils.clone(dto.getSelectV002());
		v002.setSerialNo(String.format("%03d", ++no));// 序號+1
		v002.setContent(dto.getNewContent());

		if (v002 == dto.getSelectV002()) {
			Log.v(TAG, "is equal!!");
		} else {
			Log.v(TAG, "is not equal!!");
		}

		final DBHelper help = new DBHelper(context);

		final SQLiteDatabase conn = help.getWritableDatabase();

		try {
			conn.beginTransaction();
			V002DAO dao = new V002DAO(conn, context);
			dao.insert(v002);

			conn.setTransactionSuccessful();
		} catch (Exception e) {
			Log.v(TAG, e.getMessage());
		} finally {
			conn.endTransaction();
		}
	}

	@Override
	public void deleteSubV002(MainDTO dto, Context context)
			throws IssueBussinessException {

		final V002Type type = dto.getSelectV002();

		final DBHelper help = new DBHelper(context);

		final SQLiteDatabase conn = help.getWritableDatabase();

		try {
			conn.beginTransaction();

			final V002DAO dao = new V002DAO(conn, context);

			final String uuid = type.getUuid();
			final String no = type.getSerialNo();
			dao.delete(uuid, no);

			dto.setV002s(dao.select(uuid));

			conn.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e(TAG, "e:" + e.toString());
		} finally {
			conn.endTransaction();
			conn.close();
		}

		// TODO Auto-generated method stub

	}

	@Override
	public void setMaterV002(MainDTO dto, Context context)
			throws IssueBussinessException {

		final V002Type type = dto.getSelectV002();

		final DBHelper help = new DBHelper(context);
		final SQLiteDatabase conn = help.getWritableDatabase();

		try {
			conn.beginTransaction();

			final V002DAO dao = new V002DAO(conn, context);

			final String uuid = type.getUuid();
			final String no = type.getSerialNo();
			dao.delete(uuid, no);

			dao.deleteGNo(uuid, no);

			dto.setV002s(dao.select(uuid));

			conn.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e(TAG, "e:" + e.toString());
		} finally {
			conn.endTransaction();
			conn.close();
		}

	}
}
